package pl.edu.utp.kursy.database.dao;

import java.util.ArrayList;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import pl.edu.utp.kursy.database.CourseEvent;

public class CourseEventDao extends BaseDao<CourseEvent>{
    
    public CourseEventDao(SessionFactory sessionFactory){
        super(sessionFactory);
    }

    @Override
    public CourseEvent getById(int id) {
        Session session = sessionFactory.openSession();
        
        Criteria c = session.createCriteria(CourseEvent.class);
        c.add(Restrictions.eq("id", id));    		    		
        CourseEvent event = (CourseEvent)c.uniqueResult();
        
        session.close(); 
        return event;
    }

    @Override
    public List<CourseEvent> getAll() {
        ArrayList<CourseEvent> coursesEvents;
        
        Session session = sessionFactory.openSession();
        Criteria c = session.createCriteria(CourseEvent.class);
        c.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
        coursesEvents = (ArrayList<CourseEvent>)c.list();
        session.close();
        
        return coursesEvents;  
    }
    
}
